function buscarLogin(){
   if($("#buscar").val().length==0){
       return;
   }
   $.ajax(
	{
		type: "GET",
		url: "buscarLogin",
		async: false,
                dataType: "text",
                data: "buscar="+$("#buscar").val(),
                beforeSend: function(){
                    
                },
		success: function(transport)
		{
				$("#body-content").html(transport);
                                $('input[id="btnVer"]').click(verLogin);
                                $('input[id="btnEliminar"]').click(eliminarLogin);
                                $('input[id="btnModificar"]').click(modificarLogin);
                                $('#btnAgregar').click(agregarLogin);
                                $('#btnBuscar').click(buscarLogin);
		},
                error: function (xhr, ajaxOptions, thrownError) {
                    $('#errores').html(xhr.responseText);
                    alert(thrownError);
                 }            
	});
}

function modificarLoginA(){
        if(!validaAltaLogin()){
            alert("Campos invalidos");
            return false;
        }
        
       $.ajax(
	{
		type: "POST",
		url: "modficarLogin",
		async: false,
                dataType: "text",
		data: $('#modficarLoginA').serialize(),
                beforeSend: function(){
                    
                },
		success: function(transport)
		{
				alert(transport);
                                $("#body-content").html("");
                                $('#menuLogin').trigger('click');
                               
		},
                error: function (xhr, ajaxOptions, thrownError) {
                    $('#errores').html(xhr.responseText);
                    alert(thrownError);
                 }            
	});
        $('#menuLogin').trigger('click');
        $('#menuLogin').trigger('click');
        return false;                        
}

function modificarLogin(){    
    $("#tablaLogin").hide();
    
     $.ajax(
	{
		type: "GET",
		url: "gestionModificarLoginPre",
		async: false,
                dataType: "text",
                data: "idLogin="+$(this).parent().parent().attr("id"),
                beforeSend: function(){
                    
                },
		success: function(transport)
		{
                            $("#verLogin").html(transport);
                            $(".error-inner").hide();
                            $("#verLogin").show();
                            $("#tablaLogin").hide();
                            $('#btnModificarLogin').click(modificarLoginA);
                            $("#btnRegresar").click(mostrarLogin);
		},
                error: function (xhr, ajaxOptions, thrownError) {
                    $('#errores').html(xhr.responseText);
                    alert(thrownError);
                 }            
	});
}

function eliminarLogin(){
    if(!confirm("Seguro que desea borrar la Sesion "+ $(this).parent().parent().attr("id")))
        return;
    id = $(this).parent().parent().attr("id");
    $.ajax(
	{
		type: "GET",
		url: "eliminarLogin",
		async: false,
                dataType: "text",
                data: "idLogin="+id,
                beforeSend: function(){
                    
                },
		success: function(transport)
		{
                            alert(transport); 
                            $("#"+id).remove()
		},
                error: function (xhr, ajaxOptions, thrownError) {
                    $('#errores').html(xhr.responseText);
                    alert(thrownError);
                 }            
	});
}

function verLogin(){   
   $.ajax(
	{
		type: "GET",
		url: "consultaLogin",
		async: false,
                dataType: "text",
                data: "idLogin="+$(this).parent().parent().attr("id"),
                beforeSend: function(){
                    
                },
		success: function(transport)
		{
				$("#verLogin").html(transport);
                                $("#verLogin").show();
                                $("#tablaLogin").hide();
                                $("#btnRegresar").click(mostrarLogin);
				
		},
                error: function (xhr, ajaxOptions, thrownError) {
                    $('#errores').html(xhr.responseText);
                    alert(thrownError);
                 }            
	});
}

function mostrarLogin(){
    $("#verLogin").hide();   
    $("#tablaLogin").show();
}

function agregarLogin(){  
    $("#tablaLogin").hide();
    
     $.ajax(
	{
		type: "GET",
		url: "altaLoginPre",
		async: false,
                dataType: "text",
                beforeSend: function(){
                    
                },
		success: function(transport)
		{
                            $("#verLogin").html(transport);
                            $(".error-inner").hide();
                            $("#verLogin").show();
                            $("#tablaLogin").hide();
                            $('#btnAltaLogin').click(altaLogin);
                            $("#btnRegresar").click(mostrarLogin);
		},
                error: function (xhr, ajaxOptions, thrownError) {
                    $('#errores').html(xhr.responseText);
                    alert(thrownError);
                 }            
	});
}

function altaLogin(){
       if(!validaAltaLogin())
           return false;
       
       $.ajax(
	{
		type: "POST",
		url: "altaLogin",
		async: false,
                dataType: "text",
		data: $('#altaLogin').serialize(),
                beforeSend: function(){
                    
                },
		success: function(transport)
		{
				alert(transport);
                                $("#body-content").html("");
                               
		},
                error: function (xhr, ajaxOptions, thrownError) {
                    $('#errores').html(xhr.responseText);
                    alert(thrownError);
                 }            
	});
        $('#menuLogin').trigger('click');
        $('#menuLogin').trigger('click');
        return false;
}

function validaAltaLogin(){
    $('.error-inner').hide();
    esValido = true;
    if($('#user').val().length<3){
        $("#euser").show();
        esValido = false;
    }
    if($('#pass').val().length<3){
        $("#epass").show();
        esValido = false;
    } 
    
    if($('#pass').val() != $('#pass1').val() ){
        $("#epass1").show();
        esValido = false;
    } 
    return esValido;
}